package lnkwe.util;

import com.alibaba.fastjson.JSONObject;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by administrator on 2016/4/28.
 */
public class HttpConnection {

    private int httpCode;
    private String data = "";
    private static HttpConnection  instance=null;
    private  HttpConnection(){

    }
    public static synchronized   HttpConnection getInstance(){
        if(instance==null){
            instance=new HttpConnection();
        }
        return instance;
    }
    public HttpResponseText post(String strUrl, JSONObject json) {
        HttpResponseText text = new HttpResponseText();
        HttpURLConnection conn = null;
        String result = "";
        try {
            URL url = new URL(strUrl);
            conn = (HttpURLConnection) url.openConnection();

            conn.setRequestProperty("Content-Type", "plain/text; charset=UTF-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(10000);
            conn.setRequestMethod("POST");
            conn.connect();

            String contentData = URLEncoder.encode(json.toString(), "UTF-8");
            conn.getOutputStream().write(contentData.getBytes());
            conn.getOutputStream().flush();
            conn.getOutputStream().close();

            InputStream in = conn.getInputStream();
            int httpCode = conn.getResponseCode();
            text.setHttpCode(httpCode);
            if (httpCode == 200) {
                result= FileIOUtil.getText(in);

            }
            text.setHttpCode(httpCode);
            text.setData(result);
            in.close();
        } catch (Exception e) {
            // e.printStackTrace();
            result = "http error";
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        return text;
    }

    public HttpResponseText get(String strUrl) {
        HttpResponseText text = new HttpResponseText();
        HttpURLConnection conn = null;
        String result = "";
        try {
            URL url = new URL(strUrl);
            conn = (HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(5*1000);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-type", "text/html");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestProperty("contentType", "utf-8");
            InputStream in = conn.getInputStream();
            int httpCode = conn.getResponseCode();
            System.out.println("http Code:"+httpCode);
            if (httpCode == 200) {
                result=FileIOUtil.getText(in);
            }
            text.setHttpCode(httpCode);
            text.setData(result);

        } catch (Exception e) {
            // e.printStackTrace();
            result = "http error";
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        return text;
    }


    public int getHttpCode() {
        return httpCode;
    }

    public void setHttpCode(int httpCode) {
        this.httpCode = httpCode;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }


}
